<?php

/**
 * familia actions.
 *
 * @package    espazio
 * @subpackage familia
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class familiaActions extends sfActions
{
   public function executeIndex(sfWebRequest $request)
  {
    $this->familia_list = FamiliaPeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
      // Obtener todos las titulaciones de esa familia
         $this->familia = FamiliaPeer::retrieveByPk($request->getParameter('id'));
         $this->forward404Unless($this->familia);
         
   // FORMACION
   // En el indice general mostraremos las familias de estudios de PRIMER NIVEL       
        $this->formacion_list = FamiliaPeer::getHijosFamilia($request);
       // Si la familia no tiene hijos --> Se deberian de mostrar los estudios de esa familia
        if (empty($this->formacion_list)){
            $this->formacion_list = NULL;            
        }
  }


  public function executeNew(sfWebRequest $request)
  {
    $this->form = new FamiliaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new FamiliaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($familia = FamiliaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object familia does not exist (%s).', $request->getParameter('id')));
    $this->form = new FamiliaForm($familia);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($familia = FamiliaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object familia does not exist (%s).', $request->getParameter('id')));
    $this->form = new FamiliaForm($familia);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($familia = FamiliaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object familia does not exist (%s).', $request->getParameter('id')));
    $familia->delete();

    $this->redirect('familia/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $familia = $form->save();

      $this->redirect('familia/edit?id='.$familia->getId());
    }
  }

  
}
